package org.game.msgBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections4.MapUtils;
import org.bean.ActivityEntryInfo;
import org.bean.ActivityInfo;
import org.cache.ActivityInfoCache;
import org.constant.smallkey.ActivitySmallKey;
import org.game.db.bean.Player;
import org.game.db.vo.BaseActivity;
import org.game.protobuf.s2c.S2CActivityMsg.ActivityEntryInfoMsg;
import org.game.protobuf.s2c.S2CActivityMsg.ActivityEntryInfoMsg.Builder;
import org.game.protobuf.s2c.S2CActivityMsg.ActivityInfoMsg;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.JsonManager;
import com.xiaoleilu.hutool.date.DateUnit;


/**
 * @author : DengYing
 * @CreateDate : 2017年8月15日 下午4:21:17
 * @Description ：活动消息封装
 */
public class ActivityMsgBuilder {
	
	public static List<ActivityInfoMsg.Builder> createActivityInfoMsgs(Player player,ChangeType type,List<? extends BaseActivity> activitys){
		List<ActivityInfoMsg.Builder> activityInfoMsgs = Lists.newArrayList();
		for (BaseActivity activity : activitys) {
			ActivityInfoMsg.Builder activityBuilder = createActivityInfoMsg(player,type,activity);
			if (activityBuilder!=null) {
				activityInfoMsgs.add(activityBuilder);				
			}
		}
		return activityInfoMsgs;
	}
	
	
	/**
	 * 创建一个活动更新消息
	 * @param player
	 * @param type
	 * @param activity
	 * @return
	 */
	public static ActivityInfoMsg.Builder createActivityInfoMsg(Player player,ChangeType type,BaseActivity activity) {
		ActivityInfoMsg.Builder builder = ActivityInfoMsg.newBuilder();
		builder.setCt(type);
		
		ActivityInfo activityInfo = ActivityInfoCache.getById(activity.getActivityId());
		if (activityInfo == null) {
			return null;
		}
		
		builder.setId(activityInfo.getId());
		
		// 不是删除推送才封装剩余信息
		if (!type.equals(ChangeType.DELETE)) {
			if (activityInfo.getStartDate()!=null) {
				builder.setStartTime((int)(activityInfo.getStartDate().getTime() / DateUnit.SECOND.getMillis()));	
			}
			if (activityInfo.getEndDate()!=null) {
				builder.setEndTime((int)(activityInfo.getEndDate().getTime() / DateUnit.SECOND.getMillis()));	
			}
			builder.setActivityType(activityInfo.getType());
			builder.setResetType(activityInfo.getResetType());
			builder.setExtendData(JsonManager.me().getJson().toJson(activity.getExtend()));
			builder.setRemark(JsonManager.me().getJson().toJson(activityInfo.getRemark()));
			builder.setRank(activityInfo.getRank());
			
			// 条目信息
			Map<Integer, Map<Integer, Integer>> entryMap = activity.getEntry(player.getId());
			List<Builder> entryBuilders = createActivityEntryInfoMsgs(type, entryMap ,activityInfo.getEntrys());
			for (Builder entryBuilder : entryBuilders) {
				builder.addEntrys(entryBuilder);
			}
		}
		
		return builder;
	}
	
	/** 创建活动条目消息集 */
	public static List<ActivityEntryInfoMsg.Builder> createActivityEntryInfoMsgs(ChangeType type,Map<Integer, Map<Integer,Integer>> in,Map<Integer, ActivityEntryInfo> cfgs){
		List<ActivityEntryInfoMsg.Builder> out = new ArrayList<>(in.size());
		for (Entry<Integer, ActivityEntryInfo> e : cfgs.entrySet()) {
			ActivityEntryInfoMsg.Builder entryBuilder = createActivityEntryInfoMsg(e.getValue(),in.computeIfAbsent(e.getKey(), k -> Maps.newHashMap()));
			if (entryBuilder != null) {
				out.add(entryBuilder);
			}
		}
		return out;
	}


	/** 创建活动条目消息 */
	public static ActivityEntryInfoMsg.Builder createActivityEntryInfoMsg(ActivityEntryInfo activityEntryInfo,Map<Integer, Integer> map) {
		if (activityEntryInfo == null) {
			return null;
		}
		int progress = MapUtils.getIntValue(map, ActivitySmallKey.PROGRESS,0);
		int status = MapUtils.getIntValue(map, ActivitySmallKey.STATUS,0);
		
		
		ActivityEntryInfoMsg.Builder entryBuilder = ActivityEntryInfoMsg.newBuilder();
		entryBuilder.setId(activityEntryInfo.getId());
		entryBuilder.setNowProgress(progress);
		entryBuilder.setMaxProgress(activityEntryInfo.getProgress());
		entryBuilder.setStatus(status);
		entryBuilder.setRemark(JsonManager.me().getJson().toJson(activityEntryInfo.getRemark()));
		entryBuilder.setReward(JsonManager.me().getJson().toJson(activityEntryInfo.getReward()));
		entryBuilder.setRank(activityEntryInfo.getRank());
		return entryBuilder;
	}
	
}
